package org.dfl.plugin.views.actions;

import org.dfl.plugin.DflPlugin;
import org.dfl.plugin.views.ViewsManager;
import org.dfl.plugin.views.debug.model.DataTreeElem;
import org.dfl.plugin.views.debug.model.HistoryColTreeElem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.actions.ActionDelegate;


/**
 * action that provide unselecting a element of a collection in an unnested operation in a history. 
 * It's used in a HistoriesTreeView (see mapping in plugin.xml file)
 * @author Piotr Wlodarczyk
 *
 */
public class UnselectHistoryColElemAction extends ActionDelegate{
	private IStructuredSelection selection = StructuredSelection.EMPTY;

	/* (non-Javadoc)
	 * @see org.eclipse.ui.actions.ActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
	 */
	public void selectionChanged(IAction action, ISelection sel) {
		if(sel instanceof IStructuredSelection)
			selection = (IStructuredSelection) sel;
		else 
			selection = StructuredSelection.EMPTY;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.actions.ActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	public void run(IAction action) {
		if(selection.isEmpty()) return;
		Object selObj = selection.getFirstElement();
		if(selObj instanceof DataTreeElem && ((DataTreeElem)selObj).getParent() instanceof HistoryColTreeElem){
			HistoryColTreeElem historyCol = (HistoryColTreeElem) ((DataTreeElem)selObj).getParent();
			synchronized (DflPlugin.class) {
				historyCol.setSpecialElem(null);
				historyCol.update();
			}
			ViewsManager.refreshViews(historyCol);
		}
	}
}
